#include <thread>
#include <jsoncpp/json/json.h>
#include "Util.hpp"
#include "Config.hpp"
#include "Data.hpp"
#include "Hot.hpp"
#include "Service.hpp"
void FileUtilTest(const std::string &filename)
{
    // cloud::FileUtil fu(filename);
    //  std::cout << fu.FileSize() << std::endl;
    //  std::cout << fu.LastMTime() << std::endl;
    //  std::cout << fu.LastATime() << std::endl;
    //  std::cout << fu.FileName() << std::endl;
    //  cloud::FileUtil fu(filename);
    //  std::string body;
    //  fu.GetContent(&body);
    //  cloud::FileUtil nfu("./hello.txt");
    //  nfu.SetContent(body);
    //  cloud::FileUtil fu(filename);
    //  fu.Compress(filename + ".lz");
    //  cloud::FileUtil pfu(filename + ".lz");
    //  pfu.UnCompress("./hello.txt");
    cloud::FileUtil fu(filename);
    fu.CreateDirectory();
    std::vector<std::string> arry;
    fu.GetDirectory(&arry);
    for (auto &e : arry)
    {
        std::cout << e << std::endl;
    }
}
void JsonUtilTest()
{
    Json::Value root;
    root["姓名"] = "hejiaqing";
    root["年龄"] = 18;
    root["成绩"].append(98);
    root["成绩"].append(60.5);
    root["成绩"].append(70);
    std::string str;
    cloud::JsonUtil::Serialize(root, &str);
    std::cout << str << std::endl;

    Json::Value value;
    cloud::JsonUtil::UnSerialize(str, &value);
    std::cout << value["姓名"].asString() << std::endl;
    std::cout << value["年龄"].asInt() << std::endl;
    std::cout << value["成绩"][0].asFloat() << std::endl;
    std::cout << value["成绩"][1].asFloat() << std::endl;
    std::cout << value["成绩"][2].asFloat() << std::endl;
}
void ConfigTest()
{
    cloud::Config *config = cloud::Config::GetInstance();
    std::cout << config->GetHotTime() << std::endl;
    std::cout << config->GetServerPort() << std::endl;
    std::cout << config->GetServerIp() << std::endl;
    std::cout << config->GetDownloadPrefix() << std::endl;
    std::cout << config->GetPackFileSuffix() << std::endl;
    std::cout << config->GetPackDir() << std::endl;
    std::cout << config->GetBackDir() << std::endl;
    std::cout << config->GetBackupFile() << std::endl;
}
void DataTest(const std::string &filename)
{
    cloud::BackupInfo info;
    info.NewBackupInfo(filename);
    cloud::DataManager data;
    data.Insert(info);
    cloud::BackupInfo tmp;
    data.GetOneByUrl("/download/bundle.h", &tmp);
    std::cout << tmp.pack_flag << std::endl;
    std::cout << tmp.fsize << std::endl;
    std::cout << tmp.mtime << std::endl;
    std::cout << tmp.atime << std::endl;
    std::cout << tmp.real_path << std::endl;
    std::cout << tmp.pack_path << std::endl;
    std::cout << tmp.url_path << std::endl;


    info.pack_flag = true;
    data.Update(info);
    std::vector<cloud::BackupInfo> arry;
    data.GetAll(&arry);
    for (auto &a : arry)
    {
        std::cout << a.pack_flag << std::endl;
        std::cout << a.fsize << std::endl;
        std::cout << a.mtime << std::endl;
        std::cout << a.atime << std::endl;
        std::cout << a.real_path << std::endl;
        std::cout << a.pack_path << std::endl;
        std::cout << a.url_path << std::endl;
    }

    data.GetOneRealpath(filename, & tmp);
    std::cout << tmp.pack_flag << std::endl;
    std::cout << tmp.fsize << std::endl;
    std::cout << tmp.mtime << std::endl;
    std::cout << tmp.atime << std::endl;
    std::cout << tmp.real_path << std::endl;
    std::cout << tmp.pack_path << std::endl;
    std::cout << tmp.url_path << std::endl;
}
cloud::DataManager* _data;
void HotTest()
{
    
    cloud::HotManager hot;
    hot.RunModule();
}
void ServiceTest()
{
    cloud::Service srv;
    srv.RunModule();
}
int main(int argc, char *argv[])
{
    _data = new cloud::DataManager();
    // std::string filename = argv[1];
    // FileUtilTest(filename);
    // JsonUtilTest();
    // ConfigTest();
    //DataTest(argv[1]);
    //HotTest();
    //ServiceTest();
    std::thread thread_hot_manager(HotTest);
    std::thread thread_service(ServiceTest);
    thread_hot_manager.join();
    thread_service.join();
    return 0;
}
